
# coding: utf-8

# ######################################################T1像处理函数
import os
import glob
from ..origin import Spm
from .processmethod import Process_method

#%%
class Spm_coregister_SC(Process_method,Spm):
    #作用原理：借用subprocess.Popen()函数调用afni的shell命令dispike
    #参数说明：previous_filename是input file的full path
    #参数说明：cmd_flag_parameter_list=[],因为dispike不需要参数
    #参数说明：present_filename是output file的full path
    #返回值说明：返回stdout_err即shell script的执行情况给主函数，用于日志记录。
    #举例：
    #调用：import subprocess   import nibabel as nib     import numpy as np 
    #被调用：agile_pipe
    #bug：如上所述，将来需要用for key in cmd_flag_dict.keys()和if条件语句，来解析、构造shell命令。以使脚本更加灵活。
    #modality = '_BOLD'
    #    jobs{1}.spm.spatial.coreg.estimate.ref = {other_filepaths{i}};  usually this is the BOLD/CBF modality.
    #    jobs{1}.spm.spatial.coreg.estimate.source = {T1_filepaths{i}};   usually this is the T1 modality.
    ref = r"jobs{1}.spm.spatial.coreg.estimate.ref ="   #remain stationary
    source = r"jobs{1}.spm.spatial.coreg.estimate.source ="  #be moved to match ref
    
    def concat_inputs(self):
        super().concat_inputs()
        #特有的文件路径
        print('the cbf path:',self.subject_stars)
        func_filepath = glob.glob(self.process_T1_dir + self.os_sep + self.subject_stars)[0]
        print('func_filepath:',func_filepath)
        self.ref_filepath  = self.left_brace_quation + func_filepath + self.right_brace_quation + self.semicolon
        #in the code below self.process_T1_dir is used to represent the original CBF_path
        self.source_filepath  = self.left_brace_quation +  self.present_filename + self.right_brace_quation + self.semicolon
        self.coregister_script = self.quation + self.spm_jobman_initcfg + self.ref + self.ref_filepath  + self.source + self.source_filepath + self.spm_jobman_run + self.quation
        print('self.coregister_script:',self.coregister_script)

    def concat_script(self):
        
        #cmd_1的内容：cp self.previous_filename + self.present_filename  #为了在matlab上测试，先改为window的copy命令
        self.cmd_1 = r'cp ' + self.previous_filename + self.space  + self.present_filename
        
        #cmd_2的内容：matlab -nojvm -nodesktop -nodisplay -nojvm -r  coregister_script
        self.cmd_2 = self.matlab_cmd_str  + self.coregister_script
        
        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2
#%%
class Spm_segment_SS(Process_method,Spm):
    #作用原理：借用subprocess.Popen()函数调用afni的shell命令dispike
    #参数说明：previous_filename是input file的full path
    #参数说明：cmd_flag_parameter_list=[],因为dispike不需要参数
    #参数说明：present_filename是output file的full path
    #返回值说明：返回stdout_err即shell script的执行情况给主函数，用于日志记录。
    #举例：
    #调用：import subprocess   import nibabel as nib     import numpy as np 
    #被调用：agile_pipe
    #bug：如上所述，将来需要用for key in cmd_flag_dict.keys()和if条件语句，来解析、构造shell命令。以使脚本更加灵活。
    #modality = '_BOLD'
    
    
    vols = r"jobs{1}.spm.spatial.preproc.channel.vols=" 
    write = r"jobs{1}.spm.spatial.preproc.warp.write=[0 1];"
    
    def concat_inputs(self):
        super().concat_inputs()
        #特有的文件路径
        self.vos_filepath  = self.left_brace_quation + self.present_filename + self.right_brace_quation + self.semicolon
        self.segment_script = self.quation + self.spm_jobman_initcfg + self.vols + self.vos_filepath  + self.write + self.spm_jobman_run + self.quation
        self.deformfield_filepath = os.path.split(self.present_filename)[0] + self.os_sep + self.spm_deformfield_str + os.path.split(self.present_filename)[1]
        print('self.segment_script:',self.segment_script)
        print('self.deformfield_filepath:',self.deformfield_filepath)

    def concat_script(self):
        
        #cmd_1的内容：cp self.previous_filename + self.present_filename  #为了在matlab上测试，先改为window的copy命令
        self.cmd_1 = r'cp ' + self.previous_filename + self.space  + self.present_filename
        
        #cmd_2的内容：matlab -nojvm -nodesktop -nodisplay -nojvm -r  segment_script
        self.cmd_2 = self.matlab_cmd_str  + self.segment_script
        
        
        #cmd_3的内容：rm -f   self.present_filename
        self.cmd_3 = r'rm -f ' +  self.present_filename
                
        #cmd_4的内容：ln -s  deformfield_filepath  present_filename  
        self.cmd_4 = r'ln -s ' +  self.deformfield_filepath + self.space + self.present_filename
        
        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2 + self.cmd_sep + self.cmd_3 + self.cmd_sep + self.cmd_4

        #self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2
        #下面的三行代码是用于测试怎么在命令行zhixingmatlab的。
#        self.test_script = ' "a=1; b=2; disp(a+b)" '
#        self.cmd_t = self.matlab_cmd_str + self.test_script
#        self.shell_script = self.cmd_t


# ######################################################CBF、PET像处理函数
#%%
class Spm_normalize_SN(Process_method,Spm):
    #作用原理：借用subprocess.Popen()函数调用afni的shell命令dispike
    #参数说明：previous_filename是input file的full path
    #参数说明：cmd_flag_parameter_list=[],因为dispike不需要参数
    #参数说明：present_filename是output file的full path
    #返回值说明：返回stdout_err即shell script的执行情况给主函数，用于日志记录。
    #举例：
    #调用：import subprocess   import nibabel as nib     import numpy as np 
    #被调用：agile_pipe
    #bug：如上所述，将来需要用for key in cmd_flag_dict.keys()和if条件语句，来解析、构造shell命令。以使脚本更加灵活。
    #modality = '_BOLD'
    #jobs{1}.spm.spatial.normalise.write.subj.def = {def_filepaths{i}};
    #jobs{1}.spm.spatial.normalise.write.subj.resample = {resample_filepaths{i}};
    #jobs{1}.spm.spatial.normalise.write.woptions.bb = [-90 -126 -72  
    #                                                        90 90 108];
    #jobs{1}.spm.spatial.normalise.write.woptions.vox = [2 2 2]
    
    deformfield = r"jobs{1}.spm.spatial.normalise.write.subj.def="
    resample = r"jobs{1}.spm.spatial.normalise.write.subj.resample ="
    bb_value = "a=[-90 -126 -72];b=[ 90 90 108];c(1,:)=a;c(2,:)=b;"  # c is a 2*3 array
    bb = r"jobs{1}.spm.spatial.normalise.write.woptions.bb =c;"
    vox = r'jobs{1}.spm.spatial.normalise.write.woptions.vox = [1 1 1];'
    def concat_inputs(self):
        super().concat_inputs()
        #特有的文件路径
        #self.process_T1_dir is acutally the filepath of the reference file
        print('self.modality:',self.modality)
        print('raw_deformfield_filepath:',self.process_T1_dir + self.os_sep + self.spm_deformfield_str + self.subject_stars)
        raw_deformfield_filepath =  glob.glob(self.process_T1_dir + self.os_sep + self.spm_deformfield_str + self.subject_stars)[0]
        self.deformfield_filepath  = self.left_brace_quation + raw_deformfield_filepath + self.right_brace_quation + self.semicolon
        self.resample_filepath  = self.left_brace_quation + self.present_filename + self.right_brace_quation + self.semicolon
        self.normalize_script = self.quation + self.spm_jobman_initcfg + self.deformfield + self.deformfield_filepath  + self.resample + self.resample_filepath + self.bb_value + self.bb + self.vox + self.spm_jobman_run + self.quation
        self.normalize_filepath = os.path.split(self.present_filename)[0] + self.os_sep + self.spm_normalize_str + os.path.split(self.present_filename)[1]
        print('self.normalize_script:',self.normalize_script)
        print('normalize_filepath:',self.normalize_filepath)

    def concat_script(self):
        
        #cmd_1的内容：cp self.previous_filename + self.present_filename  #为了在matlab上测试，先改为window的copy命令
        self.cmd_1 = r'cp ' + self.previous_filename + self.space  + self.present_filename
        
        #cmd_2的内容：matlab -nojvm -nodesktop -nodisplay -nojvm -r  normalize_script
        self.cmd_2 = self.matlab_cmd_str  + self.normalize_script
        
        
        #cmd_3的内容：rm -f   self.present_filename
        self.cmd_3 = r'rm -f ' +  self.present_filename
                
        #cmd_4的内容：ln -s  deformfield_filepath  present_filename  
        self.cmd_4 = r'mv  ' +  self.normalize_filepath + self.space + self.present_filename
        
        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2 + self.cmd_sep + self.cmd_3 + self.cmd_sep + self.cmd_4


#%%
class Spm_reslice_SR(Process_method,Spm):
    #作用原理：借用subprocess.Popen()函数调用afni的shell命令dispike
    #参数说明：previous_filename是input file的full path
    #参数说明：cmd_flag_parameter_list=[],因为dispike不需要参数
    #参数说明：present_filename是output file的full path
    #返回值说明：返回stdout_err即shell script的执行情况给主函数，用于日志记录。
    #举例：
    #调用：import subprocess   import nibabel as nib     import numpy as np 
    #被调用：agile_pipe
    #bug：如上所述，将来需要用for key in cmd_flag_dict.keys()和if条件语句，来解析、构造shell命令。以使脚本更加灵活。
    #modality = '_BOLD'
    #jobs{1}.spm.spatial.coreg.write.ref = {'F:\fmri\BrainMask_nii\BrainMask.nii,1'};
    #jobs{1}.spm.spatial.coreg.write.source = {'F:\fmri\Bet Fast\subj_240_1_T1.nii,1'};
    
    ref = r"jobs{1}.spm.spatial.coreg.write.ref = " 
    source = r"jobs{1}.spm.spatial.coreg.write.source ="
    
    def concat_inputs(self):
        super().concat_inputs()
        #特有的文件路径
        #self.self.temp1_dir is acutally the filepath of the reference file
        self.ref_filepath  = self.left_brace_quation + self.temp1_dir + self.right_brace_quation + self.semicolon
        self.source_filepath  = self.left_brace_quation + self.present_filename + self.right_brace_quation + self.semicolon
        self.reslice_script = self.quation + self.spm_jobman_initcfg + self.ref + self.ref_filepath  + self.source + self.source_filepath + self.spm_jobman_run + self.quation
        print('self.reslice_script:',self.reslice_script)
        self.reslice_filepath = os.path.split(self.present_filename)[0] + self.os_sep + self.spm_reslice_str + os.path.split(self.present_filename)[1]
        print('self.reslice_filepath:',self.reslice_filepath)

    def concat_script(self):
        
        #cmd_1的内容：cp self.previous_filename + self.present_filename  #为了在matlab上测试，先改为window的copy命令
        self.cmd_1 = r'cp ' + self.previous_filename + self.space  + self.present_filename
        
        #cmd_2的内容：matlab -nojvm -nodesktop -nodisplay -nojvm -r  reslice_script
        self.cmd_2 = self.matlab_cmd_str  + self.reslice_script
        
        
        #cmd_3的内容：rm -f   self.present_filename
        self.cmd_3 = r'rm -f ' +  self.present_filename
                
        #cmd_4的内容：ln -s  deformfield_filepath  present_filename  
        self.cmd_4 = r'mv  ' +  self.reslice_filepath + self.space + self.present_filename
        
        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2 + self.cmd_sep + self.cmd_3 + self.cmd_sep + self.cmd_4

#%%
class Spm_smooth_SSM(Process_method,Spm):
    #作用原理：借用subprocess.Popen()函数调用afni的shell命令dispike
    #参数说明：previous_filename是input file的full path
    #参数说明：cmd_flag_parameter_list=[],因为dispike不需要参数
    #参数说明：present_filename是output file的full path
    #返回值说明：返回stdout_err即shell script的执行情况给主函数，用于日志记录。
    #举例：
    #调用：
    #被调用：agile_pipe
    #bug：如上所述，将来需要用for key in cmd_flag_dict.keys()和if条件语句，来解析、构造shell命令。以使脚本更加灵活。
    #modality = '_BOLD'
    #jobs{1}.spm.spatial.smooth.data = {'F:\fmri\CBF\20180416_122438i5826124MRDC3DASLmacongs701a1000.nii,1'};
    #jobs{1}.spm.spatial.smooth.fwhm = [8 8 8];
    
    data = r"jobs{1}.spm.spatial.smooth.data =" 
    fwhm = r"jobs{1}.spm.spatial.smooth.fwhm ="
    
    def concat_inputs(self):
        super().concat_inputs()
        #特有的文件路径
        self.data_filepath  = self.left_brace_quation + self.present_filename + self.right_brace_quation + self.semicolon
        medi = self.left_bracket + (self.cmd_flag_parameter_list[0] + self.space)*3 + self.right_bracket
        fwhm_value = medi + self.semicolon
        print('fwhm_value:',fwhm_value)
        self.smooth_script = self.quation + self.spm_jobman_initcfg + self.data + self.data_filepath  + self.fwhm + fwhm_value + self.spm_jobman_run + self.quation
        print('self.smooth_script:',self.smooth_script)
        self.smooth_filepath = os.path.split(self.present_filename)[0] + self.os_sep + self.spm_smooth_str + os.path.split(self.present_filename)[1]
        
        

    def concat_script(self):
        
        #cmd_1的内容：cp self.previous_filename + self.present_filename  #为了在matlab上测试，先改为window的copy命令
        self.cmd_1 = r'cp ' + self.previous_filename + self.space  + self.present_filename
        
        #cmd_2的内容：matlab -nojvm -nodesktop -nodisplay -nojvm -r  smooth_script
        self.cmd_2 = self.matlab_cmd_str  + self.smooth_script
        
        
        #cmd_3的内容：rm -f   self.present_filename
        self.cmd_3 = r'rm -f ' +  self.present_filename
                
        #cmd_4的内容：ln -s  deformfield_filepath  present_filename  
        self.cmd_4 = r'mv  ' +  self.smooth_filepath + self.space + self.present_filename
        
        self.shell_script = self.cmd_1 + self.cmd_sep + self.cmd_2 + self.cmd_sep + self.cmd_3 + self.cmd_sep + self.cmd_4


#%%